package dgut.party.business.service.impl.pub;

import dgut.party.business.common.enumeration.Priority;
import dgut.party.business.common.util.PageableBuilder;
import dgut.party.business.common.util.StringUtils;
import dgut.party.business.dao.publication.ActivityPubRepository;
import dgut.party.business.dao.publication.InformationPubRepository;
import dgut.party.business.dao.publication.PublicationRepository;
import dgut.party.business.entity.publication.ActivityPublication;
import dgut.party.business.entity.publication.InformationPublication;
import dgut.party.business.entity.publication.Publication;
import dgut.party.business.service.publication.PublicationRetrieveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PublicationRetrieveServiceImpl implements PublicationRetrieveService {

  @Autowired
  PublicationRepository pubRepo;

  @Autowired
  ActivityPubRepository actPubRepo;

  @Autowired
  InformationPubRepository infoPubRepo;


  @Override
  public String getPubContent(Long id) {
    return pubRepo.findById(id)
      .orElseThrow(() -> new IllegalArgumentException("该公示不存在"))
      .getContent();
  }

  @Override
  public Publication findOneById(Long id) {
    return pubRepo.findById(id).orElseThrow(() -> new IllegalArgumentException("该公示不存在"));
  }

  @Override
  public List<Publication> findAllByTitle(String title) {
    return pubRepo.findByTitleLike(title);
  }

  @Override
  public List<Publication> findLatestTop5Pub(String pubType, Priority priority) {
    if ("info".equals(pubType))
      return pubRepo.findTop5ByTypeAndPriorityAndCreateTimeLessThan(
        InformationPublication.class.getSimpleName(), priority, new Date());
    else if ("act".equals(pubType))
      return pubRepo.findTop5ByTypeAndPriorityAndCreateTimeLessThan(
        ActivityPublication.class.getSimpleName(), priority, new Date());
    else
      return pubRepo.findTop5ByPriorityAndCreateTimeLessThan(new Date(), priority);
  }

  @Override
  public Page<Publication> allAvailablePubsPage(String keyword, String pubType, PageableBuilder pageableBuilder) {
    if (StringUtils.isBlank(keyword) && StringUtils.isBlank(pubType))
      return pubRepo.findAllAvailablePage(pageableBuilder.getPageable());
    else if (StringUtils.isBlank(keyword) && StringUtils.isNotBlank(pubType))
      return pubRepo.findAllAvailableByTypePage(pubType, pageableBuilder.getPageable());
    else {
      if ("info".equals(pubType))
        return pubRepo.findAllAvailableByTypeAndKeywordPage(keyword,
          InformationPublication.class.getSimpleName(), pageableBuilder.getPageable());
      else if ("act".equals(pubType))
        return pubRepo.findAllAvailableByTypeAndKeywordPage(keyword,
          ActivityPublication.class.getSimpleName(), pageableBuilder.getPageable());
      else return pubRepo.findAllAvailableByKeywordPage(keyword, pageableBuilder.getPageable());
    }
  }

  @Override
  public Page<Publication> allPubsPage(String keyword, String pubType, PageableBuilder pageableBuilder) {
    if (StringUtils.isBlank(keyword) && StringUtils.isBlank(pubType))
      return pubRepo.findAllPage(pageableBuilder.getPageable());
    else if (StringUtils.isBlank(keyword) && StringUtils.isNotBlank(pubType))
      return pubRepo.findAllByTypePage(pubType, pageableBuilder.getPageable());
    else {
      if ("info".equals(pubType))
        return pubRepo.findAllByTypeAndKeywordPage(keyword,
          InformationPublication.class.getSimpleName(), pageableBuilder.getPageable());
      else if ("act".equals(pubType))
        return pubRepo.findAllByTypeAndKeywordPage(keyword,
          ActivityPublication.class.getSimpleName(), pageableBuilder.getPageable());
      else return pubRepo.findAllByKeywordPage(keyword, pageableBuilder.getPageable());
    }
  }
}
